package com.block.api.controller.merchant;

import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.match.service.IDataMatchOrderInfoService;
import com.block.match.service.IDataMatchOrderItemService;
import com.block.match.service.IMatchApiBHandlerService;
import com.block.match.service.IMatchHandleService;
import com.market.member.service.IMemberSourceDictInfoService;
import com.market.member.service.IMerchantPushApiBInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/merchant")
@Validated
public class MerchantController {

    @Autowired
    IDataMatchOrderInfoService dataMatchOrderInfoService;
    @Autowired
    IMatchHandleService matchHandleService;
    @Autowired
    IDataMatchOrderItemService dataMatchOrderItemService;
    @Autowired
    IMerchantPushApiBInfoService merchantPushApiBInfoService;
    @Autowired
    IMatchApiBHandlerService matchApiBHandlerService;
    @Autowired
    IMemberSourceDictInfoService memberSourceDictInfoService;

    /**
     * 查询匹配申请列表
     * 根据订单号，查询订单列表
     */
    @NeedToken
    @RequestMapping(value = "/getMatchOrderList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getMatchOrderList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                     @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                     @RequestAttribute(value = "uri") String uri,
                                     @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                     @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                     @RequestAttribute(value = "startDate") Date startdate,
                                     @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = matchHandleService.againMatchMerchant(sessionBean.getUid(),commonParam.getChannelCode());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * api/merchant/againMatchMerchant
     * 用户推送匹配
     * 用户再次 申请匹配
     */
    @NeedToken
    @RequestMapping(value = "/againMatchMerchant", method = {RequestMethod.POST, RequestMethod.GET})
    public String againMatchMerchant(@RequestAttribute(value = "tranData") JSONObject tranData,
                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        String channelCode = commonParam.getChannelCode();
        ResponseVo responseVo = matchHandleService.againMatchMerchant(sessionBean.getUid(), channelCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 授权推送
     */
    @NeedToken
    @RequestMapping(value = "/approveToPush", method = {RequestMethod.POST, RequestMethod.GET})
    public String approveToPush(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String orderId = tranData.getString("orderId");
        String itemOrderIds = tranData.getString("itemOrderIds");
        String approveImageUrl = tranData.getString("approveImageUrl");

        ResponseVo responseVo = dataMatchOrderInfoService.approveToPush(sessionBean.getUid(), orderId, itemOrderIds, approveImageUrl);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


    /**
     * 查询匹配订单
     */
    @NeedToken
    @RequestMapping(value = "/getMatchOrderInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getMatchOrderInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                @RequestAttribute(value = "uri") String uri,
                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                @RequestAttribute(value = "startDate") Date startdate,
                                @RequestAttribute(value = "ip") String ip) throws ApiException {

        String orderNo = tranData.getString("orderNo");

        ResponseVo responseVo = matchHandleService.getMatchOrderInfo(orderNo);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 查询申请记录
     */
    @NeedToken
    @RequestMapping(value = "/getApplyRecordList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getApplyRecordList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                    @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                    @RequestAttribute(value = "uri") String uri,
                                    @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                    @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                    @RequestAttribute(value = "startDate") Date startdate,
                                    @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = dataMatchOrderItemService.getOrderSuccessList(sessionBean.getUid());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }


    /*********************** APIB  匹配订单处理 开始*************************/

//    /**
//     *
//     * 待授权列表
//     */
//    @NeedToken
//    @RequestMapping(value = "/waitApproveList", method = {RequestMethod.POST, RequestMethod.GET})
//    public String waitApproveList(@RequestAttribute(value = "tranData") JSONObject tranData,
//                                @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
//                                @RequestAttribute(value = "uri") String uri,
//                                @RequestAttribute(value = "commonParam") CommonParam commonParam,
//                                @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
//                                @RequestAttribute(value = "startDate") Date startdate,
//                                @RequestAttribute(value = "ip") String ip) throws ApiException {
//
//        String orderNo = tranData.getString("orderNo");
//
//        ResponseVo responseVo = dataMatchOrderInfoService.getWaitApproveList(sessionBean.getUid(), orderNo);
//        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
//    }

    /**
     * 待授权列表
     * api/merchant/approveClickCount
     */
//    @NeedToken
//    @RequestMapping(value = "/approveClickCount", method = {RequestMethod.POST, RequestMethod.GET})
//    public String approveClickCount(@RequestAttribute(value = "tranData") JSONObject tranData,
//                                  @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
//                                  @RequestAttribute(value = "uri") String uri,
//                                  @RequestAttribute(value = "commonParam") CommonParam commonParam,
//                                  @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
//                                  @RequestAttribute(value = "startDate") Date startdate,
//                                  @RequestAttribute(value = "ip") String ip) throws ApiException {
//
//        String orderNo = tranData.getString("orderNo");
//        String orderItemId = tranData.getString("orderItemId");
//
//        ResponseVo responseVo = dataMatchOrderInfoService.approveClickCount( orderNo,  orderItemId);
//        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
//    }

    /*********************** APIB  匹配订单处理 开始*************************/


    /**
     * 用户撞库，获取apiB的授权链接
     * 用户调用，就进行撞库申请
     *
     * 触发匹配在其他地方，不通过这个接口进行触发
     */
    @NeedToken
    @RequestMapping(value = "/handleApiBMatch", method = {RequestMethod.POST, RequestMethod.GET})
    public String checkApiBMerchant(@RequestAttribute(value = "tranData") JSONObject tranData,
                           @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                           @RequestAttribute(value = "uri") String uri,
                           @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                           @RequestAttribute(value = "commonParam") CommonParam commonParam,
                           @RequestAttribute(value = "startDate") Date startdate,
                           @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = new ResponseVo(AppRspCodeConstant.SUCCESS);
//        matchApiBHandlerService.getApiBMatchResult(sessionBean.getUid());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 返回用户当前有效的 apib订单信息
     */
    @NeedToken
    @RequestMapping(value = "/getApiBOrderList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getApiBOrderList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                    @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                    @RequestAttribute(value = "uri") String uri,
                                    @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                    @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                    @RequestAttribute(value = "startDate") Date startdate,
                                    @RequestAttribute(value = "ip") String ip) throws ApiException {
        String channelCode = commonParam.getChannelCode();
        ResponseVo responseVo = matchApiBHandlerService.getApiBApproveUrlListResult(sessionBean.getUid(),channelCode);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    @NeedToken
    @RequestMapping(value = "/getAgainApiBOrderList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getAgainApiBOrderList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                   @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = matchApiBHandlerService.getAgainApiBOrderList(sessionBean.getUid(),commonParam.getChannelCode());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 去申请 click_push_flag = 1
     */
    @NeedToken
    @RequestMapping(value = "/getApiBOrderItemClick", method = {RequestMethod.POST, RequestMethod.GET})
    public String getApiBOrderItemClick(@RequestAttribute(value = "tranData") JSONObject tranData,
                                   @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                   @RequestAttribute(value = "uri") String uri,
                                   @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                                   @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                   @RequestAttribute(value = "startDate") Date startdate,
                                   @RequestAttribute(value = "ip") String ip) throws ApiException {

        String orderItemId = tranData.getString("orderItemId");
        String infoId = tranData.getString("infoId");
        ResponseVo responseVo = matchApiBHandlerService.getApiBOrderItemClick(orderItemId,infoId);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /*********************** APIB  匹配订单处理 结束*************************/


    @RequestMapping(value = "/getMemberSourceDictList", method = {RequestMethod.POST, RequestMethod.GET})
    public String getMemberSourceDictList(@RequestAttribute(value = "tranData") JSONObject tranData,
                                        @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                                        @RequestAttribute(value = "uri") String uri,
                                        @RequestAttribute(value = "commonParam") CommonParam commonParam,
                                        @RequestAttribute(value = "startDate") Date startdate,
                                        @RequestAttribute(value = "ip") String ip) throws ApiException {


        ResponseVo responseVo = memberSourceDictInfoService.getMemberSourceDictList();
        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

}
